package com.food.platformmanagement.domain;


import com.food.platformmanagement.adapter.inbound.UserDTO;
import lombok.Data;

import javax.persistence.*;

/**
 * 所有用户（平台用户、审核员、管理员）的父类
 * @author 24270
 */
@Data
@Entity
@Table(name = "user")
public class User {
    @EmbeddedId
    private UserId id;
    @Embedded
    private UserName name;
    @Embedded
    private UserPassword password;
    private Status status;


    public User(UserName name, UserPassword password) {
        this.id = new UserId();
        this.name = name;
        this.password = password;
        this.status = Status.USER;
    }

    public User() {

    }

    /**
     * TODO 很明显，没写完
     * @param name
     */
    public void changeName(String name) {
        this.name.name(name);
    }

    public void changePassword(String password) {
        try {
            if(this.password.checkLegal(password)) {
                this.password.change(password);
            }else {
                throw new UserPasswordException("密码不合法");
            }
        }catch (Exception ignored) {
            throw new UserPasswordException("密码不合法");
        }
    }

    public void changeStatus(String newStatus) {
        switch(newStatus) {
            case "ADMIN":
                this.status = status.changeTo(Status.ADMIN);
                break;
            case "AUDITOR":
                this.status = status.changeTo(Status.AUDITOR);
                break;
            case "USER":
                this.status = status.changeTo(Status.USER);
                break;
            default:
                throw new StatusException("状态字符串无匹配！");
        }
    }

    public String id() {
        return id.value();
    }

    public String name() {
        return name.value();
    }

    public String password() {
        return password.value();
    }

    public String status() {
        return status.name();
    }

    public Status getStatus() {
        return status;
    }

    /**
     * 迎合显示，变为DTO
     * @return UserDTO
     */
    public UserDTO makeUserDTO() {
        UserDTO dto = new UserDTO();
        dto.setId(this.id());
        dto.setName(this.name());
        dto.setPassword(this.password());
        return dto;
    }

}
